package com.csdj.t309.controller;

import com.csdj.t309.contant.SessionContant;
import com.csdj.t309.entity.TSysUser;
import com.csdj.t309.entity.User;
import com.csdj.t309.service.IUserService;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;


@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    @GetMapping("/list")
    public String getUser(@RequestParam int id, Model model){
        User userById = userService.getUserById(id);
        model.addAttribute("user",userById);
        return "user/user";
    }

    @GetMapping("/login")
    public String login(){
        return "user/login";
    }
    @PostMapping("/login")
    public String doLogin(String username, String password, HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        session.setAttribute(SessionContant.USERNAME_IN_SESSION,username);
        model.addAttribute("name",username);
        model.addAttribute("phone","12345677899");
        return "user/user";
    }

    @GetMapping("/register")
    public String register(@ModelAttribute("user") TSysUser user){
        return "/user/register";
    }

    @PostMapping("/register")
    public String doRegister(@ModelAttribute("user") @Valid TSysUser user, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            bindingResult.getAllErrors().stream().forEach(item -> {
                System.out.println(item.getDefaultMessage());
            });
            return "user/register";
        }
        System.out.println(user.getAccount());
        System.out.println("注册接口");
        return "user/user";
    }




}
